function tcSetup() {
//	trSetup();
}

function cleanTree() {
	var test_tree_children = document.getElementById("test_tree_children");
	while ( ele = test_tree_children.firstChild) {
		test_tree_children.removeChild( ele);
	}
}

function collapseAll() {
	var treeView = document.getElementById('test_tree').view;
	for (var i = treeView.rowCount-1; i >= 0; i--) {
		if (treeView.isContainer(i) && treeView.isContainerOpen(i))
			treeView.toggleOpenState(i);
	}
}

function expandAll() {
	var treeView = document.getElementById('test_tree').view;
	for (var i = 0; i < treeView.rowCount; i++) {
		if (treeView.isContainer(i) && !treeView.isContainerOpen(i))
			treeView.toggleOpenState(i);
	}
}


function expandFolders() {
	expandAll();
	//var treeView = document.getElementById('test_tree').treeBoxObject.view;
	var tree = document.getElementById('test_tree');
	var treeView = tree.view;
	if (treeView.rowCount == 0)
		return;
	var i = 1;
	while( i < treeView.rowCount ) {
		if (treeView.isContainer(i)) {
			var e = treeView.isContainerEmpty(i);
			var o = treeView.isContainerOpen(i);
			if (treeView.isContainerEmpty(i) && treeView.isContainerOpen(i)) {
				alert("e"+i);
				treeView.toggleOpenState(i);
			}
			p = treeView.getParentIndex(i);
			if (!treeView.isContainerOpen(p)) {
				alert("c"+i);
				treeView.toggleOpenState(p);
			}
		} else {
			p = treeView.getParentIndex(i);
			if (treeView.isContainerOpen(p))
				treeView.toggleOpenState(p);
		}
		i++;
	}
}

function getStatusColor( stat) {
	switch (stat) {
	case "PASS":
		return "green";
	case "FAIL":
		return "red";
	case "NRUN":
		return "black";
	case "IFIF":
		return "orange";
	default:
		return "black";
	}
}

function treeSelection () {
	treeObj = {}
    var pc_tree = document.getElementById("test_tree");
	treeObj['tree'] = pc_tree;
	var pc_view = pc_tree.view;
    treeObj['view'] = pc_view;
    var row = pc_tree.currentIndex;
    if (row == -1) {
	return null;
    }
    treeObj['row'] = row;
    var tc_num = pc_view.getCellValue( row, pc_tree.columns.getColumnAt(0));
	treeObj['type'] = tc_num.substring(0,2);
	var idx = tc_num.indexOf("-");
    treeObj['level'] = pc_view.getLevel(row);
	treeObj['tc_num'] = tc_num.substring(2,idx);
	treeObj['tc_parent'] = tc_num.substring(idx+1);
    treeObj['name'] = pc_view.getCellText( row, pc_tree.columns.getColumnAt(0));
    treeObj['parent_name'] = pc_view.getCellValue( row, pc_tree.columns.getColumnAt(1));

	return treeObj;
}

function doTC( ele) {
    if ( ele.selectedIndex == 1) {
        var tree = treeSelection();
        if (treeObj == null) {
	    return;
	}
		clearAllEdit();
    }
}

function treeClick( evt) {
	var treeObj = treeSelection();
        if (treeObj == null) {
	    return;
	}
}

function treeContextClick( evt) {
	var treeObj = treeSelection();
        if (treeObj == null) {
	    return;
	}
	var tctc = document.getElementById("tree_context_newtc");
	var tcfl = document.getElementById("tree_context_newfl");
	if (treeObj.type == "tc") {
		tctc.hidden = true;
		tcfl.hidden = true;
	} else {
		tctc.hidden = false;
		tcfl.hidden = false;
	}
}

function newTC( type) {
	var treeObj = treeSelection();
        if (treeObj == null) {
	    return;
	}
	if (treeObj.type == "tc") {
		alert("You must select a folder");
		return;
	}
	//cancelTCedit();
	if (type == "fl") {
		parent_name = treeObj.name;
		num = treeObj.tc_num;
	} else {
		parent_name = treeObj.name;
		num = treeObj.tc_num;
	}
	document.getElementById("test_deck").selectedIndex  = 1;
	setParent(num, parent_name, type, 0);
	enableAllEdit();
}

function editTC() {
	var treeObj = treeSelection();
        if (treeObj == null) {
	    return;
	}
}

function deleteTC() {
	var treeObj = treeSelection();
        if (treeObj == null) {
	    return;
	}
	
	cmd = {}
	cmd['type'] = "cmd";
	cmd['cmd'] = "chgTC";
	cmd['tc_num'] = treeObj.tc_num;
	cmd['subcmd'] = "del";
	listtc_conn = new tcListener( JSON.stringify(cmd));
}

function viewDetails() {
	var treeObj = treeSelection();
        if (treeObj == null) {
	    return;
	}
	if (treeObj.type == "fl") {
		clearRun();
		document.getElementById("tr_newrun").disabled = true;
		return;
	}
	cmd = {}
	cmd['type'] = "cmd";
	cmd['cmd'] = "getTC";
	cmd['tc_num'] = treeObj.tc_num;
	cmd['parent_name'] = treeObj.parent_name;
	listtc_conn = new tcListener( JSON.stringify(cmd));
}

function fillItem( tc, count) {
	var item = document.createElement("treeitem");
	var row = document.createElement("treerow");
	item.appendChild(row);
	var cell = document.createElement("treecell");
	cell.setAttribute("label", tc['tc_name']);
	row.appendChild( cell);
	if (tc['tc_type'] == "tc") {	// test case
		item.setAttribute("open", false);
		cell.setAttribute("value", "tc"+tc['tc_num']+"-"+tc['tc_parent']);
		cell = document.createElement("treecell");
		row.appendChild( cell);
		cell.setAttribute("value", tc['parent_name']);
		cell.setAttribute("label", tc['tr_status']);
		cell.setAttribute("properties", getStatusColor(tc['tr_status']));
		switch (tc['tr_status']) {
		case 'FAIL':
			count['FAIL']++;
			break;
		case 'PASS':
			count['PASS']++;
			break;
		case 'NRUN':
			count['NRUN']++;
			break;
		case 'IFIF':
			count['IFIF']++;
			break;
		}
		cell = document.createElement("treecell");
		cell.setAttribute("label", tc['tr_date']);
		row.appendChild( cell);
		cell = document.createElement("treecell");
		cell.setAttribute("label", tc['tr_bug']);
		row.appendChild( cell);
		cell = document.createElement("treecell");
		cell.setAttribute("label", tc['tr_tester']);
		row.appendChild( cell);
		cell = document.createElement("treecell");
		cell.setAttribute("label", tc['tr_build']);
		row.appendChild( cell);
		cell = document.createElement("treecell");
		cell.setAttribute("label", tc['tr_notes']);
		row.appendChild( cell);
	} else if (tc['tc_type'] == "fl") {
		item.setAttribute("open", true);
		cell.setAttribute("value", "fl"+tc['tc_num']+"-"+tc['tc_parent']);
		cell = document.createElement("treecell");
		row.appendChild( cell);
		cell.setAttribute("value", tc['parent_name']);
		cell.setAttribute("properties", "green");
		cell = document.createElement("treecell");
		cell.setAttribute("properties", "red");
		row.appendChild( cell);
		cell = document.createElement("treecell");
		cell.setAttribute("properties", "black");
		row.appendChild( cell);
		item.setAttribute("container", "true");
		//item.setAttribute("open", "true");
	}
	
	return item;
}

function newCount() {
	c = {}
	c['PASS'] = 0;
	c['FAIL'] = 0;
	c['NRUN'] = 0;
	c['IFIF'] = 0;
	return c;
}

function addCount( c0, c1) {
	c0['PASS'] += c1['PASS'];
	c0['FAIL'] += c1['FAIL'];
	c0['NRUN'] += c1['NRUN'];
	c0['IFIF'] += c1['IFIF'];
	return c0;
}

function fillTree( up, tree, count) {
	var row;
	var item = fillItem( tree, count);
	up.appendChild(item);
	if (tree['tc_type'] == 'tc') {
			
	}
	if (tree['tc_type'] == "fl") {
		var child = document.createElement("treechildren")
		item.appendChild(child);
		var cnt = newCount();
		for (row in tree['kids']) {
			//alert(row+"\n"+tree['kids'][row])
			fillTree( child, tree['kids'][row], cnt);
		}
		item.childNodes[0].childNodes[1].setAttribute("label",cnt['PASS']);
		item.childNodes[0].childNodes[2].setAttribute("label",cnt['FAIL']);
		item.childNodes[0].childNodes[3].setAttribute("label",cnt['NRUN']);
		addCount( count, cnt);
	}
}

function startFillTree( tree) {
	var test_tree_children = document.getElementById("test_tree_children");
	var c = newCount();
	fillTree( test_tree_children, tree, c);
}

function refresh() {
	cmd = {};
	cmd['type'] = "cmd";
	cmd['cmd'] = "listTC";
	cmd['root'] = "";
	cleanTree();
	listtc_conn = new tcListener( JSON.stringify(cmd));
}

function fillTCDetail( tc) {
	document.getElementById("tr_newrun").disabled = false;
	tc.tc_runs.reverse();
	fillTCrun( tc);	
	clearAllEdit()
	//fillTCedit( tc);
}

function newTCDone( rsltObj) {
	viewDetails();	
}

function newTRDone( rsltObj) {
	var tc_num = rsltObj['tr_testcase'];
	var tree = treeSelection();
        if (tree == null) {
	    return;
	}
	refresh();
	cmd = {}
	cmd['type'] = "cmd";
	cmd['cmd'] = "getTC";
	cmd['tc_num'] = tc_num;
	//cmd['parent_name'] = treeObj.parent_name;
	listtc_conn = new tcListener( JSON.stringify(cmd));
	tree.tree.focus();
	var sel = tree.view.selection;
	sel.select(tree.row);
}

/*-----------------------------------------------------*/
/* network action
 */
tcListener.prototype = new webListener();

tcListener.prototype._onLoad =
function ( stat, text, len) {
	dump(text+"\n*************************************************\n");
	rsltObj = null;
    document.getElementById("network_in1").value = document.getElementById("network_in0").value;
    document.getElementById("network_in0").value = text;
	try {
	rsltObj = JSON.parse(text);
	} 
	catch (e) {
		alert(e)
	}
	var type = rsltObj['type'];
	var stat = rsltObj['status'];
	var cmd = rsltObj['cmd'];
	switch (cmd) {
	case "listTC":
		startFillTree(rsltObj['tree']);
		break;
	case "getTC":
		fillTCDetail(rsltObj['tc']);
		break;
	case "newTC":
		refresh();
		newTCDone(rsltObj);
		cancelTCedit();
		break;
	case "newTR":
		newTRDone(rsltObj);
		break;
	case "chgTC":
		refresh();
		cancelTCedit();
		break;
	case "chgTR":
		refresh();
		break;
	default:
		alert("invalid cmd");
	}

}

tcListener.prototype._onProgress =
function ( stat, text, len) {
    dump(text);
        var rsltObj = JSON.parse(text);
        var t= 1;
        var more = rsltObj;
}

var cmdURL = "/pc/cmd.pc"
function tcListener( data, user, pass) {
    this._url = cmdURL;
    this._user = user;
    this._pass = pass;
    document.getElementById("network_out1").value = document.getElementById("network_out0").value;
    document.getElementById("network_out0").value = data;
    this._doit( data)
}
